/*
 * @Description: 提现   CashWithdrawal
 * @version: 
 * @Company: 
 * @Author: ZhengZhuang
 * @Date: 2019-08-30 11:15:03
 * @LastEditors: zhengzhuang
 * @LastEditTime: 2020-11-18 09:21:09
 */
import 'package:C20Driver/routers/navigator_util.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:async/async.dart';
import 'package:C20Driver/service/request.dart';
import 'package:C20Driver/utils/toast.dart';
import 'package:C20Driver/widget/spinkit.dart';

class CashWithdrawal extends StatefulWidget {
  final String money;
  final String type;
  CashWithdrawal({this.money, this.type});
  @override
  _CashWithdrawalState createState() => _CashWithdrawalState();
}

class _CashWithdrawalState extends State<CashWithdrawal> {
  final AsyncMemoizer _memoizer = AsyncMemoizer();
  TextEditingController _textController = TextEditingController();

  String _name = ''; // 银行卡姓名
  String _accountNo = ''; // 银行卡账号
  String _bankLeaveMobile = ''; // 银行预留手机号
  String _bankName = ''; // 银行名称

  Future _getUserBankCard() async {
    return _memoizer.runOnce(() async {
      var dataInfo = await Request.getUserBankCard();
      setState(() {
        _accountNo = dataInfo['account_no_raw'];
        _name = dataInfo['account_name'];
        _bankLeaveMobile = dataInfo['bank_reserved_mobile'];
        _bankName = dataInfo['open_bank_name'];
      });
      return dataInfo;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('提现'),
        elevation: 0,
        actions: <Widget>[
          InkWell(
            onTap: _showTip,
            child: Center(
              child: Text(
                '提现规则   ',
                style: TextStyle(
                  color: Color(0xff3FB956),
                  fontSize: ScreenUtil().setSp(32),
                ),
              ),
            ),
          ),
        ],
      ),
      resizeToAvoidBottomPadding: false, //输入框抵住键盘
      body: FutureBuilder(
        future: _getUserBankCard(),
        builder: (BuildContext context, AsyncSnapshot snapshot) {
          if (snapshot.hasData) {
            return Container(
              width: double.infinity,
              height: double.infinity,
              color: Color(0xffffffff),
              child: Column(
                children: <Widget>[
                  Offstage(
                    offstage: _accountNo.length > 0,
                    child: _noBankCarView(),
                  ),
                  Offstage(
                    offstage: _accountNo.length == 0,
                    child: _bankCardInformation(),
                  ),
                  SizedBox(
                    height: ScreenUtil().setWidth(56.0),
                  ),
                  Divider(
                    color: Color(0xffeeeeee),
                    height: ScreenUtil().setWidth(1),
                  ),
                  SizedBox(
                    height: ScreenUtil().setWidth(26),
                  ),
                  _cashAmount(),
                  Divider(
                    color: Color(0xffeeeeee),
                    height: ScreenUtil().setWidth(1),
                  ),
                  SizedBox(
                    height: ScreenUtil().setWidth(56.0),
                  ),
                  Container(
                    width: ScreenUtil().setWidth(686.0),
                    height: ScreenUtil().setWidth(100.0),
                    decoration: BoxDecoration(
                      gradient: LinearGradient(
                        colors: [
                          Color(0xff3FB956),
                          Color(0xff65CA00),
                        ],
                      ),
                      borderRadius: BorderRadius.all(
                        Radius.circular(
                          ScreenUtil().setWidth(50),
                        ),
                      ),
                    ),
                    child: MaterialButton(
                      textColor: Color(0xffffffff),
                      child: Text(
                        '立即提现',
                        style: TextStyle(
                            fontSize: ScreenUtil().setSp(32),
                            fontWeight: FontWeight.w500),
                      ),
                      onPressed: () {
                        Request.withdrawals(context, _textController.text)
                            .then((value) {});
                      },
                    ),
                  ),
                ],
              ),
            );
          } else {
            return Spinkit();
          }
        },
      ),
    );
  }

  void _showTip() {
    showDialog<Null>(
      context: context,
      barrierDismissible: false,
      builder: (BuildContext context) {
        return AlertDialog(
          title: Text('提现规则'),
          content: SingleChildScrollView(
            child: ListBody(
              children: <Widget>[
                Text('一、提现说明'),
                Text('1、提现方式'),
                Text('目前仅支持提现到银行卡；'),
                SizedBox(height: ScreenUtil().setWidth(20.0)),
                Text('2、提现时间及金额：'),
                Text('①每日可提现一次，单笔金额不能低于1元；'),
                Text('②周一到周日上午08:00~12:00之间可提现；'),
                SizedBox(height: ScreenUtil().setWidth(20.0)),
                Text('3、到账时间：'),
                Text('当天17:30点半之前到账，周末及法定节假日等特殊情况延顺至下个工作日到账。'),
                SizedBox(height: ScreenUtil().setWidth(20.0)),
                Text('二、其他规则说明'),
                Text('1、实名认证用户提现的银行卡开户姓名必须与身份证姓名保持一致；'),
                Text('2、如若输入信息或提现银行帐号有误，提现款项将会被退回，请检查后再次申请提现；'),
              ],
            ),
          ),
          actions: <Widget>[
            FlatButton(
              child: Text('知道了'),
              onPressed: () {
                Navigator.of(context).pop();
              },
            ),
          ],
        );
      },
    ).then((val) {
      print(val);
    });
  }

  @override
  void dispose() {
    _textController.dispose();
    super.dispose();
  }

  Widget _noBankCarView() {
    return Container(
      width: ScreenUtil().setWidth(686),
      margin: EdgeInsets.only(
        top: ScreenUtil().setWidth(40),
      ),
      padding: EdgeInsets.all(ScreenUtil().setWidth(32)),
      decoration: BoxDecoration(
        color: Color(0xffF6F8FC),
        borderRadius: BorderRadius.all(
          Radius.circular(
            ScreenUtil().setWidth(32),
          ),
        ),
      ),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Text(
                '银行卡',
                style: TextStyle(
                    color: Color(0xff333333),
                    fontSize: ScreenUtil().setWidth(30),
                    fontWeight: FontWeight.w500),
              ),
              SizedBox(
                height: ScreenUtil().setWidth(16),
              ),
              Text(
                '请添加银行卡后提现',
                style: TextStyle(
                    color: Color(0xff999999),
                    fontSize: ScreenUtil().setWidth(28),
                    fontWeight: FontWeight.w400),
              ),
            ],
          ),
          InkWell(
            child: Container(
              alignment: Alignment.center,
              width: ScreenUtil().setWidth(200),
              height: ScreenUtil().setWidth(66),
              decoration: BoxDecoration(
                border: Border.all(
                    width: ScreenUtil().setWidth(1), color: Color(0xff3FB956)),
                color: Colors.transparent,
                borderRadius: BorderRadius.all(
                  Radius.circular(ScreenUtil().setWidth(33)),
                ),
              ),
              child: Text(
                '添加银行卡',
                style: TextStyle(
                  color: Color(0xff3FB956),
                  fontSize: ScreenUtil().setSp(28),
                ),
              ),
            ),
            onTap: () {
              NavigatorUtil.jump(context, '/realNameAuthentication')
                  .then((res) {
                _getUserBankCard();
              });
            },
          ),
        ],
      ),
    );
  }

  // 银行卡信息
  Widget _bankCardInformation() {
    return Container(
      width: ScreenUtil().setWidth(686.0),
      height: ScreenUtil().setWidth(268),
      padding: EdgeInsets.all(ScreenUtil().setWidth(36.0)),
      decoration: BoxDecoration(
        gradient: LinearGradient(
          colors: [
            Color(0xff5DCA88),
            Color(0xff31B669),
          ],
        ),
        borderRadius: BorderRadius.all(
          Radius.circular(
            ScreenUtil().setWidth(32),
          ),
        ),
      ),
      child: Column(
        mainAxisAlignment: MainAxisAlignment.start,
        crossAxisAlignment: CrossAxisAlignment.start,
        children: <Widget>[
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              Text(
                _bankName,
                style: TextStyle(
                    color: Color(0xffffffff),
                    fontSize: ScreenUtil().setWidth(32),
                    fontWeight: FontWeight.w500),
              ),
              InkWell(
                child: Container(
                  alignment: Alignment.center,
                  width: ScreenUtil().setWidth(178),
                  height: ScreenUtil().setWidth(54),
                  decoration: BoxDecoration(
                    border: Border.all(
                        width: ScreenUtil().setWidth(1),
                        color: Color(0xffffffff)),
                    color: Colors.transparent,
                    borderRadius: BorderRadius.all(
                      Radius.circular(ScreenUtil().setWidth(27)),
                    ),
                  ),
                  child: Text(
                    '更换银行卡',
                    style: TextStyle(
                      color: Color(0xffffffff),
                      fontSize: ScreenUtil().setSp(26),
                    ),
                  ),
                ),
                onTap: () {
                  NavigatorUtil.jump(context, '/realNameAuthentication').then(
                    (res) {
                      print('back-----');
                      _getUserBankCard();
                    },
                  );
                },
              ),
            ],
          ),
          SizedBox(
            height: ScreenUtil().setWidth(8.0),
          ),
          Text(
            '储蓄卡',
            style: TextStyle(
                color: Color(0xffffffff),
                fontSize: ScreenUtil().setWidth(28),
                fontWeight: FontWeight.w400),
          ),
          SizedBox(
            height: ScreenUtil().setWidth(50.0),
          ),
          Text(
            _accountNo,
            style: TextStyle(
              fontSize: ScreenUtil().setSp(36.0),
              color: Color(0xffffffff),
              fontWeight: FontWeight.w500,
            ),
          ),
        ],
      ),
    );
  }

  // 提现金额
  Widget _cashAmount() {
    return Container(
      width: ScreenUtil().setWidth(750.0),
      padding: EdgeInsets.all(ScreenUtil().setWidth(32.0)),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: <Widget>[
          Text(
            '可提现金额：￥${widget.money}',
            style: TextStyle(
              fontSize: ScreenUtil().setSp(30.0),
              color: Color(0xff333333),
            ),
          ),
          SizedBox(
            height: ScreenUtil().setWidth(100),
          ),
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: <Widget>[
              Expanded(
                child: Container(
                  child: TextField(
                    controller: _textController,
                    keyboardType: TextInputType.number,
                    style: TextStyle(
                      fontSize: ScreenUtil().setSp(36.0),
                      fontWeight: FontWeight.w500,
                      color: Color(0xff333333),
                    ), //输入文本的样式
                    decoration: InputDecoration(
                      hintText: '请输入提现金额',
                      hintStyle: TextStyle(
                          color: Color(0xff999999),
                          fontSize: ScreenUtil().setWidth(36),
                          fontWeight: FontWeight.w500),
                      border: InputBorder.none,
                    ),
                  ),
                ),
              ),
              InkWell(
                child: Text(
                  '全部提现',
                  style: TextStyle(
                    color: Color(0xff3FB956),
                    fontSize: ScreenUtil().setSp(30.0),
                  ),
                ),
                onTap: () {
                  setState(() {
                    _textController.text = widget.money;
                  });
                },
              ),
            ],
          ),
          // SizedBox(
          //   height: ScreenUtil().setWidth(20.0),
          // ),
        ],
      ),
    );
  }
}
